package com.iot.ammeter.view

import android.content.Context
import android.content.Intent
import android.content.res.Configuration.UI_MODE_NIGHT_YES
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.iot.ammeter.R
import com.iot.ammeter.model.entry.BalanceFillRecord
import com.iot.ammeter.model.Payment
import com.iot.ammeter.ui.theme.AmmeterAppTheme
import com.iot.ammeter.ui.components.fillrecord.BalanceCard
import com.iot.ammeter.ui.components.FillRecords
import com.iot.ammeter.viewmodel.BalanceFillViewModel
import java.util.*
import kotlin.random.Random

class BalanceFillActivity : AppCompatActivity() {

    private val vm by viewModels<BalanceFillViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            AmmeterAppTheme {
                BalanceFillScreen(vm.records.collectAsState().value) { finish() }
            }
        }
    }

    companion object {
        fun launchActivity(context: Context) {
            val intent = Intent(context, BalanceFillActivity::class.java)
            context.startActivity(intent)
        }
    }
}

/**
 * 余额充值页面。利用 [Scaffold] 作为基础脚手架，[SmallTopAppBar] 作为顶部标题栏。
 */
@Composable
private fun BalanceFillScreen(
    records: List<BalanceFillRecord>,
    navigationIconClick: () -> Unit = {}
) = androidx.compose.material.Scaffold(
    topBar = {
        SmallTopAppBar(
            title = { Text(text = stringResource(id = R.string.balance_fill_title)) },
            navigationIcon = {
                IconButton(onClick = navigationIconClick) {
                    Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "return")
                }
            },
        )
    },
    backgroundColor = MaterialTheme.colorScheme.background
) {
    Column(
        Modifier
            .fillMaxWidth()
            .padding(30.dp, 5.dp)
    ) {
        BalanceCard()
        FillRecords(records)
    }
}

@Preview("default", showBackground = true)
@Preview("night", uiMode = UI_MODE_NIGHT_YES, showBackground = true)
@Composable
private fun BalanceFillScreenPreview() = AmmeterAppTheme {
    val records = mutableListOf<BalanceFillRecord>()
    val payments = arrayOf(Payment.WeChat, Payment.AliPay)
    repeat(10) {
        records.add(
            BalanceFillRecord(
                it.toString(),
                Random.nextInt(50, 80).toFloat(),
                Date().time,
                payments.random()
            )
        )
    }
    BalanceFillScreen(records = records)
}

@Preview("no data", showBackground = true)
@Composable
private fun BalanceFillScreenNoDataPreview() = AmmeterAppTheme {
    val records = mutableListOf<BalanceFillRecord>()
    BalanceFillScreen(records = records)
}
